var maxProfit = function(prices) {
  const len = prices.length;
  if (len < 2) return 0;
  
  // 0买入 1卖出 2冷冻期
  let dp = new Array(len).fill(0).map(() => new Array(3).fill(0));
  dp[0][0] = -prices[0];

  for (let i = 1; i < len; i++) {
    dp[i][0] = Math.max(dp[i-1][0], dp[i-1][2] - prices[i]);
    dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] + prices[i]);
    dp[i][2] = dp[i-1][1];
  }

  return Math.max(dp[len-1][2], dp[len-1][1]);

};

const  prices = [1,2,3,0,2];
console.log(maxProfit(prices)); // 3